/*****************************************************************************
*    Copyright (C) 2008 ALi Corp. All Rights Reserved.
*    
*    Company confidential and Properietary information.       
*    This information may not be disclosed to unauthorized  
*    individual.    
*    File: win_storage_setting.c
*   
*    Description: 
*    
*    History: 
*    Date           Athor        Version        Reason
*    ========       ========     ========       ========
*    2008/12/18      Roman         
*        
*****************************************************************************/
#include <sys_config.h>

#include <types.h>
#include <osal/osal.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#include <hld/hld_dev.h>
#include <api/libmp/lib_mp.h>
#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "menus_root.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"
#include "osd_rsc.h"
#include "win_com_menu_define.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "storage_control.h"

/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER g_win_sto_setting;

extern CONTAINER sto_set_item_con1;
extern TEXT_FIELD sto_set_item_txtname1;
extern TEXT_FIELD sto_set_item_txtset1;
extern TEXT_FIELD sto_set_item_line1;

extern CONTAINER sto_set_item_con2;
extern TEXT_FIELD sto_set_item_txtname2;
extern TEXT_FIELD sto_set_item_txtset2;
extern TEXT_FIELD sto_set_item_line2;

extern CONTAINER sto_set_item_con3;
extern TEXT_FIELD sto_set_item_txtname3;
extern TEXT_FIELD sto_set_item_txtset3;
extern TEXT_FIELD sto_set_item_line3;

#define LINE_L_OF   0
#define LINE_W      CON_W
#define LINE_H      4

static VACTION sto_set_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT sto_set_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION sto_set_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT sto_set_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    sto_set_item_con_keymap,sto_set_item_con_callback,  \
    conobj, focusID,1)

#define LDEF_TXTNAME(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_TXTSET(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTS_SH_IDX,TXTS_HL_IDX,TXTS_SL_IDX,TXTS_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER| C_ALIGN_VCENTER, 28,0,resID,str)


#define LDEF_MM_ITEM(root,varCon,nxtObj,varTxtName,varTxtset,varLine, ID,IDu,IDd,l,t,w,h,nameID,setstr)	\
	LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxtName,1)	\
	LDEF_TXTNAME(&varCon,varTxtName,&varTxtset,0,0,0,0,0,l + TXTN_L_OF, t + TXTN_T_OF,(TXTN_W-20),TXTN_H,nameID,NULL)	\
	LDEF_TXTSET(&varCon,varTxtset, NULL/*&varLine*/,1,1,1,1,1,l + (TXTS_L_OF-20), t + TXTS_T_OF,TXTS_W,TXTS_H,0,setstr)     \
	LDEF_LINE(&varCon, varLine, NULL, 1, 1, 1, 1, 1, l + LINE_L_OF, t + CON_H, LINE_W, LINE_H, 0, 0)

LDEF_MM_ITEM(g_win_sto_setting,sto_set_item_con1, NULL,sto_set_item_txtname1,sto_set_item_txtset1,sto_set_item_line1,1,1,1,	\
				CON_L, CON_T + (CON_H + CON_GAP)*0,CON_W,CON_H,	RS_STORAGE_SELECT,NULL)

DEF_CONTAINER(g_win_sto_setting,NULL,NULL,C_ATTR_ACTIVE,0, \
    1,0,0,0,0, W_L,W_T,W_W,W_H, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    sto_set_con_keymap,sto_set_con_callback,  \
    (POBJECT_HEAD)&sto_set_item_con1, 1,0)


/*******************************************************************************
*	Local functions & variables define
*******************************************************************************/
static UINT8 m_sd_ide_statue;	// 1: sd active, 0: ide active

static void win_pvr_sd_ide_save()
{
	char hint[64];
    UINT8 back_saved;
    win_popup_choice_t choice;
	
	if(m_sd_ide_statue != sys_data_get_sd_ide_statue())
	{
		storage_switch_sd_ide_proc(m_sd_ide_statue, FALSE);
	}
}

void win_sto_set_sd_ide_set_string(UINT8 input, PTEXT_FIELD ptxt)
{
	if(input==0)
		OSD_SetTextFieldContent(ptxt, STRING_ID,RS_STORAGE_HDD);
	else
		OSD_SetTextFieldContent(ptxt, STRING_ID,RS_STORAGE_SD);
}

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/

static VACTION sto_set_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
	case V_KEY_RIGHT:
		act = VACT_INCREASE;
		break;		
	case V_KEY_LEFT:		
		act = VACT_DECREASE;
		break;		
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;		
	default:	
		act = VACT_PASS;
		break;
	}

	return act;

}


void win_sto_set_left_key(POBJECT_HEAD pObj,UINT8 id)
{
	if(id == 1)
	{
		m_sd_ide_statue = !m_sd_ide_statue;
		win_sto_set_sd_ide_set_string(m_sd_ide_statue, &sto_set_item_txtset1);
	}

	OSD_TrackObject(pObj,C_UPDATE_ALL);
}

void win_sto_set_right_key(POBJECT_HEAD pObj,UINT8 id)
{
	if(id == 1)
	{
		m_sd_ide_statue = !m_sd_ide_statue;
		win_sto_set_sd_ide_set_string(m_sd_ide_statue, &sto_set_item_txtset1);
	}
	OSD_TrackObject(pObj,C_UPDATE_ALL);
}

void win_sto_set_enter_key(POBJECT_HEAD pObj,UINT8 id)
{    
}

static PRESULT sto_set_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8	unact,input;
	UINT8	id = OSD_GetObjID(pObj);

	switch(event)
	{		
	case EVN_FOCUS_PRE_GET:
		break;
		
	case EVN_FOCUS_PRE_LOSE:
		break;

	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16) ;
        if(unact == VACT_DECREASE)
		{
			win_sto_set_left_key(pObj,id);
		}
		else if(unact == VACT_INCREASE)
		{
			win_sto_set_right_key(pObj,id);
		}		
        else if(unact == VACT_ENTER)
		{
			win_sto_set_enter_key(pObj,id);
		}
		ret = PROC_LOOP;
		break;
	}
	return ret;

}

#define VACT_RECORD_ALL     (VACT_PASS+1)

static VACTION sto_set_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
	case V_KEY_UP:
		act = VACT_CURSOR_UP;
		break;
		
	case V_KEY_DOWN:
		act = VACT_CURSOR_DOWN;
		break;		
		
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_CLOSE;
		break;
	case V_KEY_RECORD:
        act = VACT_RECORD_ALL;
        break;
	default:	
		act = VACT_PASS;
		break;
	}

	return act;
}

static PRESULT sto_set_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8	unact;
	UINT32 i;
	BOOL subt_flag;
	CONTAINER* cont = (CONTAINER*)pObj;
	UINT8	id = OSD_GetObjID(pObj);
    char new_volume[16];
	
	switch(event)
	{
	case EVN_PRE_OPEN:
		wincom_open_title((POBJECT_HEAD)&g_win_sto_setting,RS_STORAGE_SETTING, 0);
		m_sd_ide_statue = sys_data_get_sd_ide_statue();
		win_sto_set_sd_ide_set_string(m_sd_ide_statue,&sto_set_item_txtset1);
		OSD_SetContainerFocus(&g_win_sto_setting, 1);
		break;
	
	case EVN_POST_OPEN:
		break;
		
	case EVN_PRE_CLOSE:
        *((UINT32 *)param2) &= ~C_CLOSE_CLRBACK_FLG;
		win_pvr_sd_ide_save();
		sys_data_save(1);
		break;

	case EVN_POST_CLOSE:
		break;
    case EVN_UNKNOWN_ACTION:		
        break;
	}
	return ret;
}


